#include "mpHttpResponseMessageImpl.h"

#include "mpHttpResponseStringContent.h"
#include "mpHttpResponseStreamContent.h"
#include "mpHttpResponseFileContent.h"

namespace MP{
namespace HTTP{
namespace INTERNAL{

HttpResponseMessageImpl::HttpResponseMessageImpl()
    :_connectionId(""),
      _httpHeaders(),
      _responseContent(std::make_shared<HttpResponseStringContent>())
{
    //响应头中的Keepalive默认改为False。后续，将由响应中的内容来决定是否Keepalive
    _httpHeaders.setConnectionKeepalive(false);
}

HttpResponseMessageImpl::HttpResponseMessageImpl(std::stringstream& sstream)
    :_connectionId(""),
      _httpHeaders(),
      _responseContent(std::make_shared<HttpResponseStreamContent>(sstream))
{
    //响应头中的Keepalive默认改为False。后续，将由响应中的内容来决定是否Keepalive
    _httpHeaders.setConnectionKeepalive(false);
}

HttpResponseMessageImpl::HttpResponseMessageImpl(const std::string& filePath,
                                                 std::size_t fileOffset)
    :_connectionId(""),
      _httpHeaders(),
      _responseContent(std::make_shared<HttpResponseFileContent>(filePath,fileOffset))
{
    //响应头中的Keepalive默认改为False。后续，将由响应中的内容来决定是否Keepalive
    _httpHeaders.setConnectionKeepalive(false);
}

//设置HTTP连接的唯一ID
void HttpResponseMessageImpl::setConnectionId(const std::string & connectionId) {
    _connectionId = connectionId;
}

//获取HTTP连接的唯一ID
const std::string& HttpResponseMessageImpl::getConnectionId() const {
    return _connectionId;
}

//获取HTTP协议版本
const std::string& HttpResponseMessageImpl::getHttpVersion() const {
    return _http_version;
}

//获取收到的HTTP响应代码
int HttpResponseMessageImpl::getResponseCode() const{
    return _responseCode;
}

//检查收到的HTTP响应代码是否代表请求成功
bool HttpResponseMessageImpl::checkResponseCodeIsSuccess() {
    //HTTP CODE = 200 ----> 请求成功。一般用于GET与POST请求
    //HTTP CODE = 201 ----> 已创建。成功请求并创建了新的资源
    //HTTP CODE = 202 ----> 已接受。已经接受请求，但未处理完成
    //HTTP CODE = 203 ----> 非授权信息。请求成功。但返回的meta信息不在原始的服务器，而是一个副本
    //HTTP CODE = 204 ----> 无内容。服务器成功处理，但未返回内容
    //HTTP CODE = 206 ----> 部分内容。服务器成功处理了部分GET请求

    return _responseCode == 200
            || _responseCode == 201
            || _responseCode == 202
            || _responseCode == 203
            || _responseCode == 204
            || _responseCode == 206;
}

//获取服务器与状态代码一起发送的原因短语
const std::string& HttpResponseMessageImpl::getReasonPhrase() const {
    return _reasonPhrase;
}

//指示HTTP响应是否成功
bool HttpResponseMessageImpl::isSuccess() const {
    return _send_statu_code == HTTP_SEND_STATU_CODE::OK;
}

//是否使用的HTTP1.0协议
bool HttpResponseMessageImpl::isHttpVersion_1_0() const {
    return _is_http_version_1_0;
}


HTTP_SEND_STATU_CODE HttpResponseMessageImpl::getSendStatuCode() const{
    return _send_statu_code;
}

void HttpResponseMessageImpl::setSendStatuCode(HTTP_SEND_STATU_CODE sendStatuCode){
    _send_statu_code = sendStatuCode;
}

void HttpResponseMessageImpl::setReadStatus(const std::string & http_version,
                                            unsigned int responseCode,
                                            const std::string & reasonPhrase){
    _http_version = http_version;
    _responseCode = responseCode;
    _reasonPhrase = reasonPhrase;

    if(_http_version.find("1.0") != std::string::npos){
        _is_http_version_1_0 = true;
    }
}

HttpResponseHeaders& HttpResponseMessageImpl::getHttpResponseHeaders(){
    return _httpHeaders;
}


HttpResponseContent::ptr_s HttpResponseMessageImpl::getHttpResponseContent() const {
    return _responseContent;
}

}   //INTERNAL
}   //HTTP
}   //MP
